package cn.cttic.order.mapper;

import cn.cttic.order.api.domain.BaseInstallOrder;
import cn.cttic.order.api.domain.vo.BaseInstallOrderVo;
import cn.cttic.common.mybatis.core.mapper.BaseMapperPlus;
import cn.cttic.order.api.domain.vo.BaseSaleOrderVo;
import cn.cttic.order.api.domain.vo.InstallOrderVerifyInfoVo;
import cn.cttic.order.api.domain.vo.SaleOrderVerifyInfoVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * 安装订单Mapper接口
 *
 * @author Times.Studio
 * @date 2024-09-17
 */
public interface BaseInstallOrderMapper extends BaseMapperPlus<BaseInstallOrder, BaseInstallOrderVo> {

    @Select("<script>" +
        "SELECT\n" +
        "	installOrder.*, IF(biu.nick_name is null, biu.user_name, biu.nick_name) as reviewUserName, IF(biu2.nick_name is null, biu2.user_name, biu2.nick_name) as receivedUserName \n" +
        "FROM\n" +
        "	base_install_order installOrder \n" +
        "	LEFT JOIN sys_user biu ON biu.user_id = installOrder.review_user_id and biu.del_flag = 0\n" +
        "	LEFT JOIN sys_user biu2 ON biu2.user_id = installOrder.received_user_id and biu2.del_flag = 0\n" +
        "${ew.customSqlSegment}" +
        "</script>")
    public BaseInstallOrderVo getInstallOrder4VerifyReview(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);

    @Select("<script>" +
        "SELECT\n" +
        "	dictCH.dict_label AS channelName,\n" +
        "	company.company_name AS companyName,\n" +
        "	CONCAT(saleOrder.last_name, IF (saleOrder.gender = 0, '先生', IF(saleOrder.gender = 1, '女士', ''))) as fullName,\n" +
        "	product.product_name AS productName,\n" +
        "	oss.url AS productThumbnailUrl,\n" +
        "	device.device_name AS deviceName,\n" +
        "	device.device_model AS deviceModel,\n" +
        "	bu.real_name AS salesManName,\n" +
        "	biu.real_name AS installManName,\n" +
        "	installOrder.*,\n" +
        "	dictIOS.dict_label AS statusName \n" +
        "FROM\n" +
        "	base_install_order installOrder \n" +
        "	LEFT JOIN base_user biu ON biu.id = installOrder.install_man_id and biu.del_flag = 0\n" +
        "	INNER JOIN base_sale_order saleOrder ON saleOrder.id = installOrder.sale_order_id and saleOrder.del_flag = 0\n" +
        "	INNER JOIN base_user bu ON bu.id = saleOrder.sales_man_id and bu.del_flag = 0\n" +
        "	INNER JOIN base_person person ON person.id = bu.person_id and person.del_flag = 0\n" +
        "	INNER JOIN base_company company ON company.id = bu.company_id and company.del_flag = 0\n" +
        "	INNER JOIN base_product product ON product.id = saleOrder.product_id and product.del_flag = 0\n" +
        "	INNER JOIN sys_oss oss ON oss.oss_id = product.product_thumbnail\n" +
        "	INNER JOIN base_device device ON device.id = product.device_id and device.del_flag = 0\n" +
        "	INNER JOIN sys_dict_data dictCH ON dictCH.dict_type = 'dict_channel_type' \n" +
        "	AND dictCH.dict_value = company.channel_type\n" +
        "	INNER JOIN sys_dict_data dictIOS ON dictIOS.dict_type = 'dict_install_order_status' \n" +
        "	AND dictIOS.dict_value = installOrder.`status`\n" +
        "${ew.customSqlSegment}" +
        "</script>")
    public List<BaseInstallOrderVo> getInstallOrderList(@Param(Constants.WRAPPER) QueryWrapper queryWrapper);

    @Select("<script>" +
        "SELECT\n" +
        "	dictCH.dict_label AS channelName,\n" +
        "	company.company_name AS companyName,\n" +
        "	CONCAT(saleOrder.last_name, IF (saleOrder.gender = 0, '先生', IF(saleOrder.gender = 1, '女士', ''))) as fullName,\n" +
        "	product.product_name AS productName,\n" +
        "	oss.url AS productThumbnailUrl,\n" +
        "	device.device_name AS deviceName,\n" +
        "	device.device_model AS deviceModel,\n" +
        "	bu.real_name AS salesManName,\n" +
        "	biu.real_name AS installManName,\n" +
        "	installOrder.*,\n" +
        "	dictIOS.dict_label AS statusName \n" +
        "FROM\n" +
        "	base_install_order installOrder \n" +
        "	LEFT JOIN base_user biu ON biu.id = installOrder.install_man_id and biu.del_flag = 0\n" +
        "	INNER JOIN base_sale_order saleOrder ON saleOrder.id = installOrder.sale_order_id and saleOrder.del_flag = 0\n" +
        "	INNER JOIN base_user bu ON bu.id = saleOrder.sales_man_id and bu.del_flag = 0\n" +
        "	INNER JOIN base_person person ON person.id = bu.person_id and person.del_flag = 0\n" +
        "	INNER JOIN base_company company ON company.id = bu.company_id and company.del_flag = 0\n" +
        "	INNER JOIN base_product product ON product.id = saleOrder.product_id and product.del_flag = 0\n" +
        "	INNER JOIN sys_oss oss ON oss.oss_id = product.product_thumbnail\n" +
        "	INNER JOIN base_device device ON device.id = product.device_id and device.del_flag = 0\n" +
        "	INNER JOIN sys_dict_data dictCH ON dictCH.dict_type = 'dict_channel_type' \n" +
        "	AND dictCH.dict_value = company.channel_type\n" +
        "	INNER JOIN sys_dict_data dictIOS ON dictIOS.dict_type = 'dict_install_order_status' \n" +
        "	AND dictIOS.dict_value = installOrder.`status`\n" +
        "${ew.customSqlSegment}" +
        "</script>")
    public Page<BaseInstallOrderVo> getInstallOrderPageList(Page Page, @Param(Constants.WRAPPER) QueryWrapper queryWrapper);

    @Select("<script>" +
        "SELECT\n" +
        "	dictCH.dict_label AS channelName,\n" +
        "	company.company_name AS companyName,\n" +
        "	CONCAT(saleOrder.last_name, IF (saleOrder.gender = 0, '先生', IF(saleOrder.gender = 1, '女士', ''))) as fullName,\n" +
        "	product.product_name AS productName,\n" +
        "	oss.url AS productThumbnailUrl,\n" +
        "	device.device_name AS deviceName,\n" +
        "	device.device_model AS deviceModel,\n" +
        "	bu.real_name AS salesManName,\n" +
        "	biu.real_name AS installManName,\n" +
        "	installOrder.*,\n" +
        "	dictIOS.dict_label AS statusName \n" +
        "FROM\n" +
        "	base_install_order installOrder \n" +
        "	LEFT JOIN base_user biu ON biu.id = installOrder.install_man_id and biu.del_flag = 0\n" +
        "	INNER JOIN base_sale_order saleOrder ON saleOrder.id = installOrder.sale_order_id and saleOrder.del_flag = 0\n" +
        "	INNER JOIN base_user bu ON bu.id = saleOrder.sales_man_id and bu.del_flag = 0\n" +
        "	INNER JOIN base_person person ON person.id = bu.person_id and person.del_flag = 0\n" +
        "	INNER JOIN base_company company ON company.id = bu.company_id and company.del_flag = 0\n" +
        "	INNER JOIN base_product product ON product.id = saleOrder.product_id and product.del_flag = 0\n" +
        "	INNER JOIN sys_oss oss ON oss.oss_id = product.product_thumbnail\n" +
        "	INNER JOIN base_device device ON device.id = product.device_id and device.del_flag = 0\n" +
        "	INNER JOIN sys_dict_data dictCH ON dictCH.dict_type = 'dict_channel_type' \n" +
        "	AND dictCH.dict_value = company.channel_type\n" +
        "	INNER JOIN sys_dict_data dictIOS ON dictIOS.dict_type = 'dict_install_order_status' \n" +
        "	AND dictIOS.dict_value = installOrder.`status`\n" +
        "WHERE\n" +
        "   installOrder.id = #{orderId}" +
        "</script>")
    public BaseInstallOrderVo getInstallOrderInfo(@Param(value = "orderId") String orderId);

    @Select("<script>" +
        "SELECT\n" +
        "	dictCH.dict_label as salesChannelName,\n" +
        "	saleOrder.id AS orderId,\n" +
        "	CONCAT(saleOrder.last_name, IF (saleOrder.gender = 0, '先生', IF(saleOrder.gender = 1, '女士', ''))) as customerName,\n" +
        "	saleOrder.phone AS customerPhone,\n" +
        "	saleOrder.car_no AS carNo,\n" +
        "	installOrder.install_code AS installCode,\n" +
        "	company.company_name AS salesCompany,\n" +
        "	bu.real_name AS salesMan,\n" +
        "	bu.phone AS salesManPhone,\n" +
        "	bu.create_time as createTime \n" +
        "FROM\n" +
        "	base_install_order installOrder \n" +
        "	LEFT JOIN base_user biu ON biu.id = installOrder.install_man_id and biu.del_flag = 0\n" +
        "	INNER JOIN base_sale_order saleOrder ON saleOrder.id = installOrder.sale_order_id and saleOrder.del_flag = 0\n" +
        "	INNER JOIN base_user bu ON bu.id = saleOrder.sales_man_id and bu.del_flag = 0\n" +
        "	INNER JOIN base_person person ON person.id = bu.person_id and person.del_flag = 0\n" +
        "	INNER JOIN base_company company ON company.id = bu.company_id and company.del_flag = 0\n" +
        "	INNER JOIN base_product product ON product.id = saleOrder.product_id and product.del_flag = 0\n" +
        "	INNER JOIN sys_oss oss ON oss.oss_id = product.product_thumbnail\n" +
        "	INNER JOIN base_device device ON device.id = product.device_id and device.del_flag = 0\n" +
        "	INNER JOIN sys_dict_data dictCH ON dictCH.dict_type = 'dict_channel_type' \n" +
        "	AND dictCH.dict_value = company.channel_type\n" +
        "	INNER JOIN sys_dict_data dictIOS ON dictIOS.dict_type = 'dict_install_order_status' \n" +
        "	AND dictIOS.dict_value = installOrder.`status`\n" +
        "WHERE\n" +
        "   installOrder.id = #{orderId}" +
        "</script>")
    public SaleOrderVerifyInfoVo getSaleOrderVerifyInfo(@Param(value = "orderId") String orderId);

    @Select("<script>" +
        "select \n" +
        "	installOrder.id as orderId,\n" +
        "	installManCompany.company_name as installCompany,\n" +
        "	installMan.real_name as installMan,\n" +
        "	installMan.phone as installManPhone,\n" +
        "	installOrder.start_time as startInstallTime,\n" +
        "	installOrder.start_type as startInstallType\n" +
        "from \n" +
        "base_install_order installOrder\n" +
        "inner join base_sale_order saleOrder on saleOrder.id = installOrder.sale_order_id and saleOrder.del_flag = 0\n" +
        "inner join base_product product on product.id = saleorder.product_id and product.del_flag = 0\n" +
        "inner join base_device device on device.id = product.device_id and device.del_flag = 0\n" +
        "inner join base_user salesMan on salesMan.id = saleOrder.sales_man_id and salesMan.del_flag = 0\n" +
        "inner join base_company salesManCompany on salesManCompany.id = salesMan.company_id and salesManCompany.del_flag = 0\n" +
        "inner join sys_dict_data saleChannelDic on saleChannelDic.dict_value = salesManCompany.channel_type and saleChannelDic.dict_type = 'dict_channel_type'\n" +
        "inner join base_user installMan on installMan.id = installOrder.install_man_id and installMan.del_flag = 0\n" +
        "inner join base_company installManCompany on installManCompany.id = salesMan.company_id and installManCompany.del_flag = 0\n" +
        "inner join sys_dict_data installChannelDic on installChannelDic.dict_value = installManCompany.channel_type and installChannelDic.dict_type = 'dict_channel_type'\n" +
        "WHERE\n" +
        "   installOrder.id = #{orderId}" +
        "</script>")
    public InstallOrderVerifyInfoVo getInstallOrderVerifyInfo(@Param(value = "orderId") String orderId);
}
